#include "TeleportTrigger.h"
#include "Level.h"
#include "Camera.h"
#include "StringUtility.h"

#define EFFECT_DURATION 250

TeleportTrigger::TeleportTrigger(const Vector2df p, const Vector2df s) : BaseTrigger(p,s) {
}

TeleportTrigger::TeleportTrigger(const Vector2df p, const Vector2df s, CRstring parameter) : BaseTrigger(p,s) {
    vector<string> pos;
    StringUtility::tokenize(parameter,pos,",",2);
	destination = Vector2df(StringUtility::stringToFloat(pos[0]),StringUtility::stringToFloat(pos[1]));;
}

TeleportTrigger::~TeleportTrigger() {

}

//----------------------
// public declarations
//----------------------

void TeleportTrigger::hit(const SimpleDirection dir, BaseUnit* player, CRstring parameter)
{
    SimpleDirection effectDir;
    if (parameter != "noEffect")
    {
        if (player->getSpeed().x > 0) // player moves to the right, so transition also from left to right
            effectDir = diLEFT;
        else
            effectDir = diRIGHT;
        EFFECTS->wipe(EFFECT_DURATION,effectDir);
        getLevel()->logicPause(EFFECT_DURATION);
    }

    // The player hit the trigger, so we move it to the destination position
    player->setPosition(destination * (float)player->getLevel()->GetTileSize());
    CAMERA->centerOnPlayer(player);
    getLevel()->RegisterSpawnReset();

    if (parameter != "noEffect")
    {
        EFFECTS->wipe(EFFECT_DURATION,effectDir,BLACK,true);
        getLevel()->logicPause(EFFECT_DURATION);
    }
}

//----------------------
// private declarations
//----------------------

void TeleportTrigger::customUpdate() {
}
